﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhongMachTu.OConnection;
using PhongMachTu.OModel;

namespace PhongMachTu.OForm
{
    public partial class FormSetting : Form
    {
        private SettingFee fee;
        private SettingMaxPatientPerDay maxPatient;

        public FormSetting()
        {
            InitializeComponent();
        }

        public void refreshSettingValues()
        {
            fee = DataManipulate.getSettingFee();
            maxPatient = DataManipulate.getSettingMaxPatient();

            if (fee != null)
                txtMedicalFee.Text = fee._value.ToString();
            else
            {
                txtMedicalFee.Text = "0";
                fee = new SettingFee(1, 0);
            }


            if (maxPatient != null)
                txtMaxPa.Text = maxPatient._value.ToString();
            else
            {
                txtMaxPa.Text = "0";
                maxPatient = new SettingMaxPatientPerDay(0, 0);
            }
                
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (OSession.isLoggedIn())
            {
                int number;
                if (txtMedicalFee.Text != "")
                {
                    if (Int32.TryParse(txtMedicalFee.Text, out number))
                    {
                        fee._value = Convert.ToInt32(txtMedicalFee.Text);
                    }
                    else
                    {
                        fee._value = 0;
                        txtMedicalFee.Text = "0";
                    }
                }
                else
                {
                    fee._value = 0;
                }


                if (txtMaxPa.Text != "")
                {
                    if (Int32.TryParse(txtMaxPa.Text, out number))
                    {
                        maxPatient._value = Convert.ToInt32(txtMaxPa.Text);
                    }
                    else
                    {
                        maxPatient._value = 0;
                        txtMaxPa.Text = "0";
                    }
                }
                else
                {
                    maxPatient._value = 0;
                }

                if (DataManipulate.updateFee(fee) && DataManipulate.updateMaxPatient(maxPatient))
                {
                    MessageBox.Show("Cập nhật thành công");
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại");
                }
                this.refreshSettingValues();
            }
            else
            {
                FormManager.settingForm.Hide();
                FormManager.loginForm.Show();
                MessageBox.Show("Quá thời gian chờ cho phép, vui lòng đăng nhập lại");
            }
        }

        private void FormSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            FormManager.settingForm.Hide();
            FormManager.mainForm.Show();
            FormManager.mainForm.Focus();
        }
    }
}
